<template>
  <div class="container">
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>师资管理</el-breadcrumb-item>
      <el-breadcrumb-item>咨询师等级管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="等级名称">
          <el-input
            v-model="queryInfo.consultLvName"
            clearable
            placeholder="请输入等级名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList">搜索</el-button>
          <el-button type="primary" @click="toAddConsultLv">添加</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="ConsultLvList" border style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="consultLvId" label="ID"></el-table-column>
      <el-table-column prop="consultLvName" label="等级名称"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toEditConsultLv(scope.row)">编辑</el-link>
          <el-link type="primary" @click="deleteConsultLv(scope.row.consultLvId)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增和修改的对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="consultLv">
        <el-form-item label="等级名称" label-width="80px">
          <el-input
            placeholder="请输入等级名称"
            v-model="consultLv.consultLvName"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addConsultLv">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询信息对象
      queryInfo: {
        consultLvName: "",
        pageNum: 1,
        pageSize: 5,
      },
      //等级对象
      consultLv: {
        consultLvId: 0,
        consultLvName: "",
      },

      total: 183,
      ConsultLvList: [],
      dialogFormVisible: false,
      title: "",
    };
  },

  methods: {
    //点击修改打开对话框
    toEditConsultLv(row){
      this.consultLv = row;
      this.title = "修改等级";
      this.dialogFormVisible = true;
    },
    //点击添加打开对话框
    toAddConsultLv() {
      this.title = "添加等级";
      this.dialogFormVisible = true;
    },
    // 添加或修改功能
    addConsultLv() {
      if (this.consultLv.consultLvId) {
        //修改
        this.$http.put("consultLv/editConsultLv",this.consultLv).then(res=>{
            if (res.data.status == 200) {
              this.$message.success("修改成功！");
              //清除数据
              this.consultLv = {
                consultLvId: "",
                consultLvName: "",
              };
              //关闭对话框
              this.dialogFormVisible = false;
              //重新查询
              this.queryList();
            } else {
              this.$message.error("修改失败！");
            }

        })
      } else {
        //添加
        this.$http.post("consultLv/addConsultLv", this.consultLv).then(res=>{
            if (res.data.status == 200) {
              this.$message.success("添加成功！");
              //清除数据
              this.consultLv = {
                consultLvId: "",
                consultLvName: ""
              };
              //关闭对话框
              this.dialogFormVisible = false;
              //重新查询
              this.queryList();
            } else {
              this.$message.error("添加失败！");
            }
          })
      }
    },

    // 删除
 deleteConsultLv(id) {
   this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
     confirmButtonText: '确定',
     cancelButtonText: '取消',
     type: 'warning'
  })
  .then(() => {
      this.$http.delete('consultLv/removeConsultLv/' + id)
          .then(resp => {
          if (resp.data.status == 200) {
             this.$message.success(resp.data.message)
              this.queryList()
           } else {
             this.$message.error(resp.data.message)
            }
           })
       })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    //取消事件
    cancle() {
      this.dialogFormVisible = false;
      this.consultLv = {
        consultLvName: "",
      };
    },

    //分页查询的方法
    queryList() {
      //调用后端接口获取数据
      this.$http
        .get("consultLv/findByPageConsultLv", {
          params: this.queryInfo,
        })
        .then((res) => {
          if (res.data.status == 200) {
            console.log(res.data);
            this.ConsultLvList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败！");
          }
        });
    },

    handleSizeChange(size) {
      //   console.log("每页显示数量发生改变");
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      //重新查询
      this.queryList();
    },
    handleCurrentChange(num) {
      //   console.log("页码发生改变");
      this.queryInfo.pageNum = num;
      //重新查询
      this.queryList();
    },
  },

  // 页面加载时就查询数据
  mounted() {
    this.queryList();
  },
};
</script>

<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 5px;
}
</style>